package com.example.csv2db;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;

public class StudentFieldSetMapper implements FieldSetMapper<Student> {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    public Student mapFieldSet(FieldSet fieldSet) throws BindException {
        // Instantiating the report object
        Student student = new Student();

        try {
            // Setting the fields (s_id, s_name, s_sex, s_birthday, s_phone)
            student.setS_id(fieldSet.readInt("s_id"));
            student.setS_name(fieldSet.readString("s_name"));
            student.setS_sex(fieldSet.readString("s_sex"));
            student.setS_birthday(fieldSet.readString("s_birthday"));
            student.setS_phone(fieldSet.readString("s_phone"));
        } catch (Exception e) {
            logger.error("Error:" + e.getMessage());
            throw e;
        }

        logger.info("New student:" + student.toString());
        //logger.debug("测试test: debug");
        //logger.warn("测试test: warning");
        //logger.error("测试test: error");

        return student;
    }
}